﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataAccess
{
    public class DAUser
    {

        ShopOnlineDataContext context = new ShopOnlineDataContext();

        public user insertUser(string username, string fullname, string description, string password, bool isAdmin)
        {
            user newUser = new user() {username = username, fullname = fullname, description = description, password = password, isAdmin = isAdmin};
            
            context.users.InsertOnSubmit(newUser);
            context.SubmitChanges();
            

            return newUser;
        }
        

        public user updateProfile(int id, string fullname, string description, string password)
        {
            user editUser = context.users.Single(u=> u.ID == id);
            editUser.fullname = fullname;
            editUser.description = description;
            editUser.password = password;

            context.SubmitChanges();
            return editUser;
        }

        public user deleteUser(int id) {
            user deleteUser = context.users.Single(u => u.ID == id);
            context.users.DeleteOnSubmit(deleteUser);
            context.SubmitChanges();
            return deleteUser;
        }

        public IList<user> selectUser()
        {
            return context.users.ToList();
        }

        public user selectUserByID(int ID)
        {
            user selectUser = context.users.Single(u => u.ID == ID);
            return selectUser;
        }

        public UserPubInfo checkLogin(string username, string password) {
            user newUser = context.users.Single(u=> u.username == username && u.password == password);
            //newUser.isAdmin
            UserPubInfo userInfo;
            if (newUser != null)
            {
                userInfo = new UserPubInfo(newUser.username, newUser.fullname, newUser.description, newUser.isAdmin);
            }
            else
            {
                userInfo = new UserPubInfo();
            }
            return userInfo;
        }
    }
}
